package ManHinhChinh;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Rectangle;

public class DangNhap {

	protected Shell shell;
	private Text txt_Acc;
	private Text txt_Pass;

	/**
	 * Launch the application.
	 * @param args
	 */
	public Boolean blDangNhap = false ;
	public String _txtpass;
	public String _txtacc;
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation(x,y);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() 
	{
		shell = new Shell();
		shell.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode==13)
				{
					_txtpass = txt_Pass.getText().trim();
					_txtacc = txt_Acc.getText().trim();

					if (_txtacc.equals("")) {
						MessageBox MsbAcc = new MessageBox(shell,
								SWT.ICON_ERROR | SWT.OK);
						MsbAcc.setText("Error");
						MsbAcc.setMessage("Bạn chưa nhập tên tài khoản ");
						MsbAcc.open();
						return;

					}

					if (_txtpass.equals("")) {
						MessageBox MsbAcc = new MessageBox(shell,
								SWT.ICON_ERROR | SWT.OK);
						MsbAcc.setText("Error");
						MsbAcc.setMessage("Bạn chưa nhập mật khẩu ");
						MsbAcc.open();
						return;
					}
					DataUser du = new DataUser();
					boolean tp = du.testPassword(_txtacc, _txtpass);
					if (tp == true) {
						blDangNhap = true;
						shell.close();
					} else {
						MessageBox Msbam = new MessageBox(shell, SWT.ICON_ERROR
								| SWT.OK);
						Msbam.setText("Error");
						Msbam.setMessage("Bạn đã nhập sai tên tài khoản hoặc mật khẩu... Vui lòng nhập lại ");
						Msbam.open();
						return;
					}
				}
			}
		});
		
		
		shell.setSize(450, 300);
		shell.setText("Ch\u01B0\u01A1ng Tr\u00ECnh Qu\u1EA3n L\u00FD B\u00E1n H\u00E0ng");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Times New Roman", 20, SWT.BOLD));
		lblNewLabel.setBounds(153, 21, 140, 39);
		lblNewLabel.setText("\u0110\u0103ng Nh\u1EADp ");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.NORMAL));
		lblNewLabel_1.setBounds(61, 80, 75, 25);
		lblNewLabel_1.setText("Account");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Times New Roman", 15, SWT.NORMAL));
		lblNewLabel_2.setBounds(61, 131, 90, 25);
		lblNewLabel_2.setText("Password");
		
		Button btnLogin = new Button(shell, SWT.NONE);
		btnLogin.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		btnLogin.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\DN.png"));
	
		btnLogin.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) 
			{
				 _txtpass = txt_Pass.getText().trim();
				 _txtacc = txt_Acc.getText().trim();
			    
				if(_txtacc.equals(""))
				{
					MessageBox MsbAcc = new MessageBox(shell,SWT.ICON_ERROR|SWT.OK );
					MsbAcc.setText("Error");
					MsbAcc.setMessage("Bạn chưa nhập tên tài khoản ");
					MsbAcc.open();
					return;
					
				}
					
				if(_txtpass.equals(""))
						{
								MessageBox MsbAcc = new MessageBox(shell,SWT.ICON_ERROR|SWT.OK );
								MsbAcc.setText("Error");
								MsbAcc.setMessage("Bạn chưa nhập mật khẩu ");
								MsbAcc.open();
								return;
						} 
			DataUser du = new DataUser();	
			boolean tp = du.testPassword(_txtacc, _txtpass);
				if (tp == true) {
					blDangNhap = true;
					shell.close();
				} else {
					MessageBox Msbam = new MessageBox(shell, SWT.ICON_ERROR
							| SWT.OK);
					Msbam.setText("Error");
					Msbam.setMessage("Bạn đã nhập sai tên tài khoản hoặc mật khẩu... Vui lòng nhập lại ");
					Msbam.open();
					return;
				}
				
			}
		});
		btnLogin.setFont(SWTResourceManager.getFont("Times New Roman", 12, SWT.BOLD));
		btnLogin.setBounds(60, 212, 140, 39);
		btnLogin.setText("\u0110\u0103ng Nh\u1EADp");
		
		Button btnExit = new Button(shell, SWT.NONE);
		btnExit.setImage(SWTResourceManager.getImage("..\\_QuanLyBanHang\\image\\exit.ico"));
		btnExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageBox MsbThoat = new MessageBox(shell,SWT.ICON_WARNING|SWT.OK | SWT.CANCEL);
				MsbThoat.setText("Exit?");
				MsbThoat.setMessage("Bạn có muốn thoát chương trình ");
				int returnCode = MsbThoat.open();
				if (returnCode == SWT.OK) {
					System.exit(0);
				}
						
			}
		});
		btnExit.setFont(SWTResourceManager.getFont("Times New Roman", 13, SWT.BOLD));
		btnExit.setAlignment(SWT.LEFT);
		btnExit.setBounds(248, 212, 127, 39);
		btnExit.setText("   Tho\u00E1t");
		
		txt_Acc = new Text(shell, SWT.BORDER);
		txt_Acc.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode==13)
				{
					_txtpass = txt_Pass.getText().trim();
					_txtacc = txt_Acc.getText().trim();

					if (_txtacc.equals("")) {
						MessageBox MsbAcc = new MessageBox(shell,
								SWT.ICON_ERROR | SWT.OK);
						MsbAcc.setText("Error");
						MsbAcc.setMessage("Bạn chưa nhập tên tài khoản ");
						MsbAcc.open();
						return;

					}

					if (_txtpass.equals("")) {
						MessageBox MsbAcc = new MessageBox(shell,
								SWT.ICON_ERROR | SWT.OK);
						MsbAcc.setText("Error");
						MsbAcc.setMessage("Bạn chưa nhập mật khẩu ");
						MsbAcc.open();
						return;
					}
					DataUser du = new DataUser();
					boolean tp = du.testPassword(_txtacc, _txtpass);
					if (tp == true) {
						blDangNhap = true;	
						shell.close();
					} else {
						MessageBox Msbam = new MessageBox(shell, SWT.ICON_ERROR
								| SWT.OK);
						Msbam.setText("Error");
						Msbam.setMessage("Bạn đã nhập sai tên tài khoản hoặc mật khẩu... Vui lòng nhập lại ");
						Msbam.open();
						return;
					}
				}
			}
		});
		txt_Acc.setBounds(187, 80, 148, 21);
		txt_Acc.forceFocus();
		
		txt_Pass = new Text(shell,SWT.PASSWORD |SWT.BORDER);
		txt_Pass.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode==13)
				{
					_txtpass = txt_Pass.getText().trim();
					_txtacc = txt_Acc.getText().trim();

					if (_txtacc.equals("")) {
						MessageBox MsbAcc = new MessageBox(shell,
								SWT.ICON_ERROR | SWT.OK);
						MsbAcc.setText("Error");
						MsbAcc.setMessage("Bạn chưa nhập tên tài khoản ");
						MsbAcc.open();
						return;

					}

					if (_txtpass.equals("")) {
						MessageBox MsbAcc = new MessageBox(shell,
								SWT.ICON_ERROR | SWT.OK);
						MsbAcc.setText("Error");
						MsbAcc.setMessage("Bạn chưa nhập mật khẩu ");
						MsbAcc.open();
						return;
					}
					DataUser du = new DataUser();
					boolean tp = du.testPassword(_txtacc, _txtpass);
					if (tp == true) {
						blDangNhap = true;
						shell.close();
					} else {
						MessageBox Msbam = new MessageBox(shell, SWT.ICON_ERROR
								| SWT.OK);
						Msbam.setText("Error");
						Msbam.setMessage("Bạn đã nhập sai tên tài khoản hoặc mật khẩu... Vui lòng nhập lại ");
						Msbam.open();
						return;
					}
				}
			}
		});
		txt_Pass.setBounds(187, 131, 148, 21);

	}
}
